<?php
namespace Admin\Controller;
use Think\Controller;
class DeviceController extends CommonController{

    // 分类 列表
    public function cate(){
        session('page_size',$_GET['page_size']);
        $page_no = I('get.p') ? I('get.p') : 1;
        $page_size = I('get.page_size') ? I('get.page_size') : 10;
        $data = M('device_cate')
            ->where()
            ->page($page_no, $page_size)
            ->select();
        $count =M('device_cate')->where()->count();
        $Page = new \AntAge\Page($count, $page_size);
        $show = $Page->show();
        
        $this->assign('type',$_SESSION['type']);
        $this->assign('page', $show);
        $this->assign("count", $count);
        $this->assign("data", $data);
        $this->display();
    }
    // 分类 添加
    public function cateAdd(){
        if ($_POST) {
            $pest = implode(',', $_POST['pest_cate']);
            $data = array(
                'cate_name' => I('post.cate_name'),
                'add_time'  => time(),
                'status'    => I('post.status'),
                'explanation'=> I('post.explanation'),
                'pest'      => $pest,
            );
            //检查是否为空
            if (!$_POST['pest_cate']) {
                $this->error('请选择需要捕获的害虫');
            }
            if (!$data['cate_name']) {
                $this->error('请填写类型名称');
            }
            //检查分类是否重复
            $cate_name = M('device_cate')->getField('cate_name',true);
            if (in_array($data['cate_name'], $cate_name)) {
                $this->error('此分类已经存在');
            }
            //添加数据
            $res = M('device_cate')->add($data);
            if ($res) {
                $this->success("添加成功");
            }else{
                $this->error('添加失败');
            }
        }else{
            $animal = M('animal')->select();
            $this->assign('type',$_SESSION['type']);
            $this->assign('animal',$animal);
            $this->display();
        }
    }

}
